package com.xm.xmapp;

import java.util.HashMap;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.camera.CameraHelper;
import com.xm.xmapp.camera.OnCaptureCallback;
import com.xm.xmapp.customview.MaskSurfaceView;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.response.bean.ResponseDriverBean;
import com.xm.xmapp.response.bean.ResponseIdCardBean;
import com.xm.xmapp.tools.ImageTool;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.Tools;

public class TakePhotoActivity extends BaseActivity implements OnCaptureCallback {
	private MaskSurfaceView surfaceview;
	private ImageView imageView, iv_take_photo;

	private Button btn_recapture;// 重拍
	private Button btn_ok;// 确认
	private RelativeLayout rl_bottom;
	private HashMap<String, String> map;
	boolean isIdCard;// 是否是身份证,还是驾驶证正面
	Bitmap bitmap = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_takephoto);
	}

	public void initView() {
		surfaceview = (MaskSurfaceView) findViewById(R.id.surface_view);
		imageView = (ImageView) findViewById(R.id.image_view);
		btn_recapture = (Button) findViewById(R.id.btn_recapture);
		iv_take_photo = (ImageView) findViewById(R.id.iv_take_photo);
		btn_ok = (Button) findViewById(R.id.btn_ok);
		rl_bottom = (RelativeLayout) findViewById(R.id.rl_bottom);
	}

	public void initData() {
		// 设置矩形区域大小
		setMyTitle("实名认证");
		int distence = getResources().getDimensionPixelSize(R.dimen.dp15);
		int width = ScreenUtils.getScreenWidth(this) - 2 * distence;
		this.surfaceview.setMaskSize(width, width * 3 / 5);
		map = new HashMap<String, String>();
		map.put("api_key", CodeConstant.api_key);
		map.put("api_secret", CodeConstant.api_secret);
		isIdCard = getIntent().getBooleanExtra("isIdCard", false);

	}

	public void initEvent() {

		// 拍照
		iv_take_photo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				CameraHelper.getInstance().tackPicture(TakePhotoActivity.this);
				// rl_bottom.setVisibility(View.VISIBLE);
			}
		});

		// 重拍
		btn_recapture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				rl_bottom.setVisibility(View.GONE);
				imageView.setVisibility(View.GONE);
				surfaceview.setVisibility(View.VISIBLE);
				CameraHelper.getInstance().startPreview();
			}
		});

		// 识别
		btn_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				promptDialog.show();
				String url = null;
				if (isIdCard) {
					url = "https://api-cn.faceplusplus.com/cardpp/v1/ocridcard";

					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
							Type.A_Authorize.GET_BITMAP, bitmap);
//					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
//							Type.A_Authorize.GET_DATA);

					finish();

				} else {
					url = "https://api-cn.faceplusplus.com/cardpp/v1/ocrdriverlicense";

					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
							Type.A_Authorize.GET_BITMAP, bitmap);
//					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
//							Type.A_Authorize.GET_DATA);

					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeDriverActivity.class,
							Type.A_Authorize_Driver.GET_BITMAP, bitmap);
//					MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeDriverActivity.class,
//							Type.A_Authorize_Driver.GET_DATA);

					finish();
				}
				// MyAppcation.getMyAppcation().getPostData(TakePhotoActivity.this,
				// url, map,
				// new GetDataInterFace<String>() {
				//
				// @Override
				// public void onResponse(String response) {
				// promptDialog.dismiss();
				// if (isIdCard) {
				// ResponseIdCardBean bean = JSON.parseObject(response,
				// ResponseIdCardBean.class);
				// if (bean.cards != null && bean.cards.size() > 0
				// && !bean.cards.get(0).id_card_number.equals("")) {
				//
				// MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
				// Type.A_Authorize.GET_BITMAP, bitmap);
				// MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
				// Type.A_Authorize.GET_DATA, bean.cards.get(0));
				//
				// finish();
				// } else {
				// Tools.showMessage("识别失败，请重新拍照");
				// }
				// } else {
				// ResponseDriverBean bean = JSON.parseObject(response,
				// ResponseDriverBean.class);
				// if (bean.cards != null && bean.cards.size() > 0
				// && !bean.cards.get(0).license_number.equals("")) {
				// MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
				// Type.A_Authorize.GET_BITMAP, bitmap);
				// MyAppcation.getMyAppcation().notifyDataSetChanged(AuthorizeActivity.class,
				// Type.A_Authorize.GET_DATA, bean.cards.get(0));
				//
				// MyAppcation.getMyAppcation().notifyDataSetChanged(
				// AuthorizeDriverActivity.class,
				// Type.A_Authorize_Driver.GET_BITMAP,
				// bitmap);
				// MyAppcation.getMyAppcation().notifyDataSetChanged(
				// AuthorizeDriverActivity.class,
				// Type.A_Authorize_Driver.GET_DATA,
				// bean.cards.get(0));
				//
				// finish();
				// } else {
				// Tools.showMessage("识别失败，请重新拍照");
				// }
				// }
				// }
				// }, new DisssmissInterFace() {
				//
				// @Override
				// public void putError(VolleyError error) {
				// promptDialog.dismiss();
				// }
				// });

			}
		});

	}

	@Override
	public void onCapture(boolean success, String filepath) {

		String message = "拍照成功";
		if (!success) {
			message = "拍照失败";
			CameraHelper.getInstance().startPreview();
			this.imageView.setVisibility(View.GONE);
			this.surfaceview.setVisibility(View.VISIBLE);
			rl_bottom.setVisibility(View.GONE);
		} else {
			rl_bottom.setVisibility(View.VISIBLE);
			this.imageView.setVisibility(View.VISIBLE);
			this.surfaceview.setVisibility(View.GONE);
			int width = ScreenUtils.getScreenWidth(this);
			int height = ScreenUtils.getScreenHeight(this);
			if (width > 720) {
				bitmap = ImageTool.getSmallBitmap(CodeConstant.PhotoPath, 720, 1280);
			} else {
				bitmap = ImageTool.getSmallBitmap(CodeConstant.PhotoPath, width, height);
			}
			map.put("image_base64", ImageTool.Bitmap2StrByBase64(bitmap));
			this.imageView.setImageBitmap(bitmap);
		}

	}
}
